﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
// Additional imports
using System.Web.Security;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace MyNamespace
{
    /// <summary>
    /// Summary description for MembershipController
    /// </summary>
    [DataObject(true)]
    public class MembershipController
    {
        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static MembershipUserCollection GetAllUsers()
        {
            return Membership.GetAllUsers();
        } // end of GetAllUsers

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static DataTable GetAllFriends(Guid myUserId)
        {
            SqlDataAdapter adapter = GetAdapter("SELECT * FROM Friendships WHERE MyUserID = @MyUserID");
            AddParameter(adapter, "@MyUserID", SqlDbType.UniqueIdentifier, myUserId);
            return GetTableFromAdapter(adapter);
        } // end of GetAllFriends()

        public static bool IsFriend(Guid myUserId, Guid myFriendID)
        {
            return FindFriend(myUserId, myFriendID).Rows.Count > 0;
        } // end of IsFriend()

        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public static DataTable FindFriend(Guid myUserId, Guid friendUserId)
        {
            SqlDataAdapter adapter = GetAdapter("SELECT * FROM Friendships WHERE MyUserID = @MyUserID AND MyFriendID = @FriendUserID");
            AddParameter(adapter, "@MyUserID", SqlDbType.UniqueIdentifier, myUserId);
            AddParameter(adapter, "@FriendUserID", SqlDbType.UniqueIdentifier, friendUserId);
            return GetTableFromAdapter(adapter);
        } // end of FindFriend()

        public static void AddFriend(Guid myUserID, Guid friendID)
        {
            SqlConnection conn = new SqlConnection(GetConn());
            SqlCommand cmd = new SqlCommand("INSERT INTO Friendships (MyUserID, MyFriendID, Status) Values (@MyUserID, @FriendID, 'Accepted')", conn);
            cmd.Parameters.Add("@MyUserID", SqlDbType.UniqueIdentifier).Value = myUserID;
            cmd.Parameters.Add("@FriendID", SqlDbType.UniqueIdentifier).Value = friendID;
            conn.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
#region PrivateMethods
		private static DataTable GetTableFromAdapter(SqlDataAdapter adapter)
        {
                    DataSet virtualDB = new DataSet();
                    adapter.Fill(virtualDB, "Friendships");
                    return virtualDB.Tables["Friendships"];
        }

        private static void AddParameter(SqlDataAdapter adapter, string ParamName, SqlDbType ParamType, Object value)
        {
            adapter.SelectCommand.Parameters.Add(ParamName, ParamType).Value = value;
        }

        private static SqlDataAdapter GetAdapter(string SelectCommand)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(SelectCommand, GetConn());
            return adapter;
        }

        private static string GetConn()
        {
            return ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        }
#endregion 
	} // end of Class
} // end of Namespace